﻿Imports MySQL
Imports MySQLCommand
Imports System.Data.SqlClient
Imports System.Configuration

Public Class FrmCongtyKH
    Sub Connect()
        clsSQL.setConnection(ConfigurationSettings.AppSettings("connect"))
        If clsSQL.Connected() = False Then
            MsgBox("Lỗi kết nối")
            'MsgBox("Kết nối thành công")
        End If
    End Sub
    Private Sub btnThemCty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemCty.Click
        If macty = 0 Then 'insert cty
            If txtTenCty.Text <> "" Then
                clsSQL.ExecReader("sp_Congty_Insert", txtTenCty.Text, txtDiachiCty.Text, txtDienthoaiCty.Text, txtEmailCty.Text, txtFaxCty.Text)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                Macdinh()
                LoadCty()
            End If
        Else 'update cty
            If txtTenCty.Text <> "" Then
                clsSQL.ExecReader("sp_Congty_Update", macty, txtTenCty.Text, txtDiachiCty.Text, txtDienthoaiCty.Text, txtEmailCty.Text, txtFaxCty.Text)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                Macdinh()
                LoadCty()
            End If
        End If
    End Sub
    Dim macty As Integer = 0
    Dim mactynguoimua As Integer = 0
    Dim suanguoimua As Boolean = False
    Dim manm As Integer = 0
    Sub LoadCty()
        DsCongty.Tables("sp_Congty_Select").Clear()
        clsSQL.FillDataSet("sp_Congty_Select", DsCongty, "sp_Congty_Select")
    End Sub
    Sub Macdinh()
        txtTenCty.Text = ""
        txtDiachiCty.Text = ""
        txtDienthoaiCty.Text = ""
        txtEmailCty.Text = ""
        txtFaxCty.Text = ""
        macty = 0
        btnThemCty.BackgroundImage = ASIA.My.Resources.Resources.add
    End Sub
    Private Sub btnHuyCty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuyCty.Click
        Macdinh()
    End Sub

    Private Sub FrmKhachHang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connect()
        LoadCty()
    End Sub

    Private Sub dtgCty_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgCty.DoubleClick
        macty = dtgCty.Item(0, dtgCty.CurrentRow.Index).Value
        txtTenCty.Text = dtgCty.Item(1, dtgCty.CurrentRow.Index).Value.ToString
        txtDiachiCty.Text = dtgCty.Item(2, dtgCty.CurrentRow.Index).Value.ToString
        txtDienthoaiCty.Text = dtgCty.Item(3, dtgCty.CurrentRow.Index).Value.ToString
        txtEmailCty.Text = dtgCty.Item(4, dtgCty.CurrentRow.Index).Value.ToString
        txtFaxCty.Text = dtgCty.Item(5, dtgCty.CurrentRow.Index).Value.ToString
        btnThemCty.BackgroundImage = ASIA.My.Resources.Resources.save

    End Sub

    Private Sub mniXoaCty_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mniXoaCty.Click
        macty = dtgCty.Item(0, dtgCty.CurrentRow.Index).Value
        clsSQL.ExecReader("sp_Congty_Delete", macty)
        clsSQL.getData.Read()
        MsgBox(clsSQL.getData("Err"))
        Macdinh()
        LoadCty()
    End Sub
    Sub LoadNguoimua()
        DsCongty.Tables("sp_NguoiMua_Select").Clear()
        clsSQL.FillDataSet("sp_NguoiMua_SelectByMaCt", DsCongty, "sp_NguoiMua_Select", macty)
    End Sub
    Private Sub bttnXemNguoiMua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnXemNguoiMua.Click

        LoadNguoimua()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnEditNguoiMua.Click
        'MsgBox(suanguoimua & " -> " & Not suanguoimua)
        suanguoimua = Not suanguoimua
        If suanguoimua = True Then

            mactynguoimua = macty
            cbbCongty.Text = txtTenCty.Text
            grpNguoiMua.Enabled = True

        Else
            MacdinhNguoiMua()
            grpNguoiMua.Enabled = False

        End If
        
    End Sub
    Sub MacdinhNguoiMua()
        txtTenNguoiMua.Text = ""
        txtDtNguoiMua.Text = ""
        txtEmailNguoiMua.Text = ""
        grpNguoiMua.Enabled = False
        cbbCongty.Text = ""
        suanguoimua = False
        manm = 0
        mactynguoimua = 0
        bttnThemNguoimua.BackgroundImage = ASIA.My.Resources.Resources.add
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MacdinhNguoiMua()
    End Sub

    
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnThemNguoimua.Click
        If manm = 0 Then ' insert
            If txtTenNguoiMua.Text <> "" Then
                clsSQL.ExecReader("sp_NguoiMua_Insert", txtTenNguoiMua.Text, txtDtNguoiMua.Text, txtEmailNguoiMua.Text, mactynguoimua)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                MacdinhNguoiMua()
                LoadNguoimua()
            End If
        Else 'update
            If txtTenNguoiMua.Text <> "" Then
                clsSQL.ExecReader("sp_NguoiMua_Update", manm, txtTenNguoiMua.Text, txtDtNguoiMua.Text, txtEmailNguoiMua.Text, mactynguoimua)
                clsSQL.getData.Read()
                MsgBox(clsSQL.getData("Err"))
                MacdinhNguoiMua()
                LoadNguoimua()

            End If
        End If
    End Sub

    Private Sub cbbCongty_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbCongty.SelectedIndexChanged
        mactynguoimua = cbbCongty.SelectedValue
    End Sub

    Private Sub dtgNguoiMua_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dtgNguoiMua.DoubleClick
        If dtgNguoiMua.Rows.Count > 0 And suanguoimua = True Then

            manm = dtgNguoiMua.Item(0, dtgNguoiMua.CurrentRow.Index).Value
            txtTenNguoiMua.Text = dtgNguoiMua.Item(1, dtgNguoiMua.CurrentRow.Index).Value.ToString
            txtDtNguoiMua.Text = dtgNguoiMua.Item(2, dtgNguoiMua.CurrentRow.Index).Value.ToString
            txtEmailNguoiMua.Text = dtgNguoiMua.Item(3, dtgNguoiMua.CurrentRow.Index).Value.ToString
            mactynguoimua = dtgNguoiMua.Item(4, dtgNguoiMua.CurrentRow.Index).Value.ToString
            bttnThemNguoimua.BackgroundImage = ASIA.My.Resources.Resources.save

        End If
    End Sub
End Class